package org.nanotek.lucene.document;

import java.io.Serializable;

import org.apache.log4j.Logger;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.FieldType;
import org.nanotek.Base;
import org.nanotek.base.Configuration;
import org.nanotek.cms.domain.lucene.config.document.FieldConfiguration;
import org.nanotek.BuilderException;

@SuppressWarnings("serial")
public class FieldBuilderBase <T extends Base<K>,K extends Serializable> extends FieldBuilder<T> {

	private static final Logger log = Logger.getLogger(FieldBuilder.class);
	private T id;

	public FieldBuilderBase(T baseObject) {
		this.id = baseObject;
	}

	@Override
	public Field build() {
		return super.build();
	}

	@Override
	public Field build(Configuration<FieldConfiguration> baseParameters) {
		Field configuratedField = null;
		try { 
			//			Set<ConstraintViolation<FieldConfiguration>> constraintViolations = validator.validate(baseParameters.get());
			//			if (constraintViolations.size() == 0) { 
			FieldType luceneFieldType = new FieldType ();
			luceneFieldType.setStored((baseParameters.get().getStore() == Store.YES)?true:false);
			luceneFieldType.setNumericType(baseParameters.get().getNumericType());
			luceneFieldType.setStoreTermVectors(baseParameters.get().getStoreTermVector());
			luceneFieldType.setStoreTermVectorOffsets(baseParameters.get().getStorePositionWithTermVector());
			luceneFieldType.setStoreTermVectorPositions(baseParameters.get().getOmitTermFreqAndPositions());
			luceneFieldType.setIndexed(baseParameters.get().getIndexed());
			luceneFieldType.setOmitNorms(baseParameters.get().getOmitNorms());
			configuratedField = new Field (baseParameters.get().getName(), getId().toString(), luceneFieldType);
			//			} 
		}catch (Exception ex) 
		{ 
			log.debug (ex);
			throw new BuilderException(ex);
		}
		return configuratedField;
	}
	

	public T getId() {
		return id;
	}

	public void setId(T id) {
		this.id = id;
	}
}
